<template>
  <div class="wrap">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 搜索框 -->
      <el-row :gutter="20" style="margin-bottom: 15px">
        <el-col :span="8">
          <el-input
            v-model.trim="search.query"
            placeholder="请输入内容"
            clearable
            maxlength="10"
            @clear="handleClear"
            @keyup.enter.native="handleSearch"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="handleSearch"
            />
          </el-input>
        </el-col>
      </el-row>
      <!-- table表格 -->
      <el-table
        :data="orderList"
        max-height="410px"
        border
        style="width: 100%"
      >
        <el-table-column
          prop="order_number"
          label="序号"
          width="50"
          align="center "
        >
          <template slot-scope="scope">
            {{ (search.pagenum - 1)*search.pagesize+scope.$index+1 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="order_number"
          label="订单编号"
        />
        <el-table-column
          prop="order_price"
          label="订单价格"
          width="80"
        />
        <el-table-column
          prop="pay_status"
          label="是否付款"
          width="100"
        >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.order_pay === '0'" type="danger">未付款</el-tag>
            <el-tag v-else>已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="is_send"
          label="是否发货"
          width="80"
        />
        <el-table-column
          prop="create_time"
          label="下单时间"
          width="180"
        >
          <template slot-scope="scope">
            {{ scope.row.create_time|setTime }}
          </template>
        </el-table-column>
        <el-table-column
          width="180"
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button size="small" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)" />
            <el-button size="small" type="success" icon="el-icon-location-outline" @click="handleAdress" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :current-page="search.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="search.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
      <!-- 修改地址弹出框 -->
      <el-dialog title="修改地址" :visible.sync="editFormVisible" width="50%" @close="editCancle()">
        <el-form ref="eidtFormRef" :model="eidtForm" label-width="90px" :rules="eidtFormRules">
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader
              v-model="eidtForm.address1"
              :options="cityData"
              :props="props"
              @change="handleChange"
            />
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="eidtForm.address2" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editCancle">取 消</el-button>
          <el-button type="primary" @click="editeComfirm">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 查看地址 -->
      <el-dialog title="收货地址" :visible.sync="addressFormVisible">
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in progressInfo"
            :key="index"
            :timestamp="activity.time"
          >
            {{ activity.context }}
          </el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import cityData from './citydata.js';
import { getOrdersApi, getAddressApi } from '@/api/order';
export default {
  name: 'Order',
  components: { },
  data() {
    return {
      progressInfo: [], // 物流信息
      addressFormVisible: false, // 查看地址
      cityData,
      props: {
        lable: 'label',
        value: 'value',
        children: 'children'
      },
      eidtFormRules: {
        address1: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      eidtForm: { // 编辑form
        address1: '',
        address2: ''
      },
      editFormVisible: false, // 修改地址是否显示
      // 搜素框
      search: {
        query: '',
        pagenum: 1, // 当前页面
        pagesize: 5// 每页显示条数
      },
      orderList: [], // 订单列表
      total: 0// 总条数
    };
  },
  mounted() {
    this.getOrder();
  },
  methods: {
    handleAdress() {
      getAddressApi().then((res) => {
        const { data, meta } = res.data;
        if (meta.status !== 200) return this.$message.error(meta.message);
        this.$message.success(meta.message);
        this.progressInfo = data;
      });
      this.addressFormVisible = true;
    },
    handleChange() {

    },
    // 修改按钮
    handleEdit() {
      this.editFormVisible = true;
    },
    // 修改地址取消
    editCancle() {
      this.$refs.eidtFormRef.resetFields();
      this.editFormVisible = false;
    },
    // 修改地址确定
    editeComfirm() {
      this.editFormVisible = false;
    },
    //   监听每页条数
    handleSizeChange(val) {
      this.search.pagesize = val;
      this.getOrder();
    },
    //   监听当前页
    handleCurrentChange(val) {
      this.search.pagenum = val;
      this.getOrder();
    },
    // 订单数据列表
    getOrder() {
      getOrdersApi(this.search).then((res) => {
        const { data, meta } = res.data;
        if (meta.status !== 200) return this.$message.error(meta.msg);
        this.orderList = data.goods;
        this.total = data.total;
      });
    },
    // 清空搜索框
    handleClear() {
      this.getOrder();
    },
    // 搜素
    handleSearch() {
      this.getOrder();
    }
  }
};
</script>
<style scoped>
</style>
